// 引入 axios
import axios from 'axios';

// 创建 axios 实例
const service = axios.create({
  baseURL: '/api', // 设置基础 URL
  timeout: 5000 // 设置超时时间
});

// 请求拦截器
service.interceptors.request.use(
  config => {
    //后期提交token
    let token = sessionStorage.getItem('author_token') 
    if(token){
      config.headers['author_token'] = token;
    }
    // 在发送请求之前做些什么
    return config;
  },
  error => {
    console.error('Request error:', error);
    return Promise.reject(error);
  }
);

// 响应拦截器
service.interceptors.response.use(
  response => {
    // 对响应数据做点什么
    const { data } = response;
    if (data.code !== 200) {
      console.error('Response error:', data);
      return Promise.reject(new Error(data.message || 'Error'));
    }
    return data;
  },
  error => {
    // 对响应错误做点什么
    console.error('Response error:', error);
    return Promise.reject(error);
  }
);

/**
 * Performs a GET request to the specified URL with optional query parameters.
 * @async
 * @param {string} url - The URL to send the GET request to
 * @param {Object} [params={}] - Optional query parameters as key-value pairs
 * @returns {Promise<Object>} The response data from the request
 * @throws {Error} If the request fails, throws the original error after logging details
 */
export const get = async (url, params = {}) => {
  try {
    const response = await service.get(url, { params });
    return response;
  } catch (error) {
     console.error('GET request failed:', {
      url,
      params,
      error: error.message,
      stack: error.stack
    });
    throw error;
  }
};

/**
 * Sends a POST request to the specified URL with optional data.
 * @async
 * @param {string} url - The URL to send the request to.
 * @param {Object} [data={}] - Optional data to send with the request.
 * @returns {Promise<Object>} The response from the server.
 * @throws {Error} If the request fails.
 */
export const post = async (url, data = {}) => {
    try {
    const response = await service.post(url, data);
    return response;
  } catch (error) {
    console.error('GET request failed:', {
      url,
      data,
      error: error.message,
      stack: error.stack
    });
    throw error;
  }
};

/**
 * Sends a PUT request to the specified URL with the given data.
 * @async
 * @param {string} url - The URL to send the request to.
 * @param {Object} [data={}] - The data to send with the request.
 * @returns {Promise<Object>} The response from the server.
 * @throws {Error} If the request fails.
 */
export const put = async (url, data = {}) => {
  try {
    const response = await service.put(url, data);
    return response;
  } catch (error) {
    console.error('GET request failed:', {
      url,
      params,
      error: error.message,
      stack: error.stack
    });
    throw error;
  }
};

// 封装 DELETE 请求
/**
 * Sends a DELETE request to the specified URL with optional query parameters.
 * @async
 * @param {string} url - The URL to send the request to
 * @param {Object} [params={}] - Optional query parameters as key-value pairs
 * @returns {Promise<Object>} The response data from the server
 * @throws {Error} If the request fails
 */
export const del = async (url, params = {}) => {
  try {
    const response = await service.delete(url, { params });
    return response;
  } catch (error) {
    console.error('GET request failed:', {
      url,
      params,
      error: error.message,
      stack: error.stack
    });
    throw error;
  }
};

// 导出 service 实例
export default service;